<?php
class WorkerAction extends CommonAction {

    public function _initialize()
    {

    }

    public function index()
    {
        $worker = M('Worker');
        parent::page($worker->count());
        $data = $worker->limit($this->limit)
                       ->order('id desc')
                       ->select();

        $this->assign('data', $data);
        $this->display();
    }

    public function add()
    {
        $worker = M('Worker');
        if(isset($_POST['send']))
        {
            $this->validate(self::$ADD_SEC);

            $data['worker_name'] = $this->_post('worker_name');
            $data['worker_department'] = $this->_post('worker_department');
            $data['worker_group'] = $this->_post('worker_group');
            if($worker->add($data))
            {
                $content = "用户：".session('admin')." 添加了一个工人信息 姓名：$data[worker_name]
                            部门：$data[worker_department] 班组： $data[worker_group]";
                addAdminRecord($content);
                redirectByModal('添加工人信息成功!', self::$SUCCESS, __URL__ . '/index');
            }
        }
        $this->display();
    }

    public function addByExcel()
    {
        $worker = M('Worker');

        if(isset($_POST['send']))
        {
            if($this->_post('up_file') == '')
                redirectByModal('请先选择excel文件', self::$ERROR);
        }

        $this->display();
    }

    public function ajaxUpload()
    {
        $session_name = session_name();
        if (isset($_POST[$session_name]))
        {
            session_id($_POST[$session_name]);
            session_start();
        }

        $worker = M('Worker');

        import('ORG.Util.ExcelReader');
        import('ORG.Util.UploadFile');

        $up = new UploadFile('up_file', 2000000, './Application/TempExcel/');

        // 读取excel
        $data = new Spreadsheet_Excel_Reader();
        $data->setOutputEncoding('UTF-8');
        $data->read($up->getLinkPath());

        for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++)
        {
            $workerData['worker_name'] = $data->sheets[0]['cells'][$i][1];
            $workerData['worker_department'] = $data->sheets[0]['cells'][$i][2];
            $workerData['worker_group'] = $data->sheets[0]['cells'][$i][3];

            $worker->add($workerData);
        }

        // 删除excel
        unlink($up->getLinkPath());
        rmdir(dirname($up->getLinkPath()));
    }

    public function update($id)
    {
        $worker = M('Worker');
        $currentData = $worker->where('id='.$id) ->find();

        if(isset($_POST['send']))
        {
            $this->validate(self::$UPDATE_SEC);

            $data['worker_name'] = $this->_post('worker_name');
            $data['worker_department'] = $this->_post('worker_department');
            $data['worker_group'] = $this->_post('worker_group');
            $data['id'] = $this->_post('id');

            $content = "用户：".session('admin')." 修改了一个工人信息 姓名：$data[worker_name]
                        部门：$data[worker_department] 班组： $data[worker_group]";

            $worker->save($data);
            addAdminRecord($content);
            redirectByModal('修改工人信息成功!', self::$SUCCESS, __URL__.'/index');
        }
        $this->assign('data', $currentData);
        $this->display();
    }

    public function delete()
    {
        $id = $this->_post('id');
        $worker = M('Worker');
        $data = $worker->where('id='.$id)->find();

        if($worker->where('id='.$id)->delete())
        {
            $content = "用户：".session('admin')." 删除了一个工人信息 名称：$data[worker_name] 部门：$data[worker_department] 班组： $data[worker_group]";
            addAdminRecord($content);
            redirectByModal('删除工人成功!', self::$SUCCESS, __URL__ . '/index');
        }
    }

    // 验证
    private function validate($sec)
    {
        $worker_name = $this->_post('worker_name');
        $worker_department = $this->_post('worker_department');
        $worker_group = $this->_post('worker_group');

        if($worker_name == '')
            redirectByModal('工人姓名必须填写!', self::$ERROR);
        else if(mb_strlen($worker_name, 'utf-8') > 5)
            redirectByModal('工人姓名不能超过5位!', self::$ERROR);
        else if($worker_department == -1)
            redirectByModal('所在部门必须选择!', self::$ERROR);
        else if($worker_group == -1)
            redirectByModal('所在班组必须选择!', self::$ERROR);
    }
}